/*
 *  Netzpult - Your desk on the web
 *  Copyright (C) 2011  Ulrich Hilger, http://uhilger.de
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/
 */

package de.uhilger.netzpult.server;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author Copyright (c) Ulrich Hilger, http://uhilger.de
 * @author Published under the terms and conditions of
 * the <a href="http://www.gnu.org/licenses/" target="_blank">GNU General Public License</a>
 */
public class VirtualPage extends HttpServlet {

	private static final long serialVersionUID = -761994053850623108L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//test(req, resp);
		production(req, resp);
	}
	
	private void production(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Writer w = resp.getWriter();
		String pathInfo = req.getPathInfo();
		if (pathInfo != null) {
			String[] pathElements = pathInfo.split("/");
			if (pathElements != null) {
				int length = pathElements.length;
				if (length > 1) {
					String pathPrefix = getPathDepthPrefix(pathElements.length - 1);
					netzpult(w, pathPrefix);
				} else {
					netzpult(w, "");
				}
			} else {
				netzpult(w, "");
			}
		} else {
			netzpult(w, "");
		}
	}

	private void test(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Writer w = resp.getWriter();
		w.write("<html>");
		w.write("<head><title>netzpult</title></head>");
		w.write("<body>");

		String pathInfo = req.getPathInfo();

		w.write("<p>");
		w.write("request.getPathInfo = ");
		w.write(pathInfo);
		w.write("</p>");

		w.write("<p>Path elements:<br>");
		String[] pathElements = pathInfo.split("/");
		for (int i = 0; i < pathElements.length; i++) {
			w.write(pathElements[i]);
			w.write("<br>");
		}
		w.write("</p>");

		w.write("<p>");
		w.write("pathElements.length = ");
		w.write(Integer.toString(pathElements.length));
		w.write("</p>");

		w.write("</body");
		w.write("</html>");
		w.flush();
		w.close();
	}

	private void netzpult(Writer w, String pathPrefix) throws IOException {
		w.write("<!doctype html>");
		w.write("<html>");
		w.write("<head>");
		w.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">");
		w.write("<link type=\"text/css\" rel=\"stylesheet\" href=\"");
		w.write(pathPrefix);
		w.write("Netzpult.css\">");
		w.write("<title>Netzpult</title>");
		w.write("<script type=\"text/javascript\" language=\"javascript\" src=\"");
		w.write(pathPrefix);
		w.write("netzpult/netzpult.nocache.js\"></script>");
		w.write("<script type=\"text/javascript\" src=\"");
		w.write(pathPrefix);
		w.write("tiny_mce/tiny_mce.js\"></script>");
		w.write("</head>");
		w.write("<body>");
		w.write("<iframe src=\"javascript:''\" id=\"__gwt_historyFrame\" tabIndex='-1' style=\"position: absolute; width: 0; height: 0; border: 0\"></iframe>");
		w.write("<noscript>");
		w.write("<div style=\"width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif\">");
		w.write("Your web browser must have JavaScript enabled in order for this");
		w.write("application to display correctly.</div>");
		w.write("</noscript>");
		w.write("</body>");
		w.write("</html>");
	}

	private static String getPathDepthPrefix(int depth) {
		StringBuffer buf = new StringBuffer();
		String prefix = "../";
		for (int i = 0; i < depth; i++) {
			buf.append(prefix);
		}
		return buf.toString();
	}

}
